/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.translog.gui.outro;

import br.com.translog.fachada.FachadaTranslog;
import br.com.translog.modelo.SaidaEntrega;
import javax.swing.JOptionPane;

public class FrmLoginUsuario extends javax.swing.JFrame {

    /**
     * Creates new form FrmLoginUsuario
     */
    public FrmLoginUsuario() {
        initComponents();
        setLocationRelativeTo(null);
    }
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlLoginUsuario = new javax.swing.JPanel();
        lblMatricula = new javax.swing.JLabel();
        txtMatricula = new javax.swing.JTextField();
        lblSenha = new javax.swing.JLabel();
        pwdSenha = new javax.swing.JPasswordField();
        btnAcessar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        sepLogin = new javax.swing.JSeparator();
        lblCodigoRastreioEntrega = new javax.swing.JLabel();
        txtCodigoRastreioEntrega = new javax.swing.JTextField();
        btnRastrear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TransLog - Sistema de Entrega de Transportadora");
        setMinimumSize(new java.awt.Dimension(554, 250));
        setName("frmLoginUsuario"); // NOI18N
        setResizable(false);

        pnlLoginUsuario.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Área de Acesso ao Sistema e Rastreamento de Entrega", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        lblMatricula.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblMatricula.setText("Matricula");

        txtMatricula.setToolTipText("Entre com o login.");
        txtMatricula.setNextFocusableComponent(pwdSenha);

        lblSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblSenha.setText("Senha");

        pwdSenha.setToolTipText("Entre com a senha.");
        pwdSenha.setNextFocusableComponent(btnAcessar);

        btnAcessar.setMnemonic('A');
        btnAcessar.setText("Acessar");
        btnAcessar.setToolTipText("Realiza o login no sistema.");
        btnAcessar.setName(""); // NOI18N
        btnAcessar.setNextFocusableComponent(btnCancelar);
        btnAcessar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcessarActionPerformed(evt);
            }
        });

        btnCancelar.setMnemonic('C');
        btnCancelar.setText("Cancelar");
        btnCancelar.setToolTipText("Fecha a janela e sai do sistema.");
        btnCancelar.setNextFocusableComponent(txtCodigoRastreioEntrega);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        sepLogin.setOrientation(javax.swing.SwingConstants.VERTICAL);

        lblCodigoRastreioEntrega.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblCodigoRastreioEntrega.setText("Código Rastreio Entrega");

        txtCodigoRastreioEntrega.setToolTipText("Insira o código de rastreio da entrega.");
        txtCodigoRastreioEntrega.setName(""); // NOI18N
        txtCodigoRastreioEntrega.setNextFocusableComponent(btnRastrear);

        btnRastrear.setMnemonic('R');
        btnRastrear.setText("Rastrear");
        btnRastrear.setToolTipText("Realiza o rastreamento da entrega.");
        btnRastrear.setNextFocusableComponent(txtMatricula);
        btnRastrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRastrearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlLoginUsuarioLayout = new javax.swing.GroupLayout(pnlLoginUsuario);
        pnlLoginUsuario.setLayout(pnlLoginUsuarioLayout);
        pnlLoginUsuarioLayout.setHorizontalGroup(
            pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMatricula)
                    .addComponent(lblSenha))
                .addGap(18, 18, 18)
                .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(pwdSenha, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlLoginUsuarioLayout.createSequentialGroup()
                        .addComponent(btnAcessar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar))
                    .addComponent(txtMatricula))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(sepLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlLoginUsuarioLayout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlLoginUsuarioLayout.createSequentialGroup()
                                .addComponent(btnRastrear)
                                .addGap(101, 101, 101))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlLoginUsuarioLayout.createSequentialGroup()
                                .addComponent(txtCodigoRastreioEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30))))
                    .addGroup(pnlLoginUsuarioLayout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(lblCodigoRastreioEntrega))))
        );
        pnlLoginUsuarioLayout.setVerticalGroup(
            pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginUsuarioLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMatricula)
                    .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCodigoRastreioEntrega))
                .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlLoginUsuarioLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(pwdSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSenha))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlLoginUsuarioLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigoRastreioEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlLoginUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAcessar)
                        .addComponent(btnCancelar))
                    .addComponent(btnRastrear))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(pnlLoginUsuarioLayout.createSequentialGroup()
                .addComponent(sepLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 46, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlLoginUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlLoginUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAcessarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcessarActionPerformed
        
        //Verificando se os campos de login e senha estão em "branco"...
        if (!txtMatricula.getText().equals("") && pwdSenha.getPassword().length != 0) {
            //Chamando o método de validação e passando os valores digitados como parâmentros...
            if (!FachadaTranslog.getInstFachadaTranslog().validaLogin(Integer.parseInt(txtMatricula.getText()), 
                                                                       new String(pwdSenha.getPassword()))) {
                JOptionPane.showMessageDialog(null, "Matrícula ou Senha Incorreta!");
                //Limpa os campos...
                txtMatricula.setText("");
                pwdSenha.setText("");
            } else {
//Salvando a sessao, chamando o Menu Principal e fechando a tela de Login...
FachadaTranslog.getInstFachadaTranslog().salvaSessaoFuncionario(Integer.parseInt(txtMatricula.getText()));
new FrmMenuPrincipal().setVisible(true);
this.setVisible(false);
}
        } else {
            JOptionPane.showMessageDialog(null, "Matrícula ou Senha não Informada!");
            //Limpa os campos...
            txtMatricula.setText("");
            pwdSenha.setText("");
        }
    }//GEN-LAST:event_btnAcessarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnRastrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRastrearActionPerformed
        
        SaidaEntrega ss;
            
        if (!txtCodigoRastreioEntrega.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Codigo não informado.");
        } else {    
            
            ss = FachadaTranslog.getInstFachadaTranslog().consultaRastreamanto(Integer.parseInt(txtCodigoRastreioEntrega.getText()));
            
            if (ss == null){            
                JOptionPane.showMessageDialog(null, " Codigo não localizado.");            
            } else{
            
            }
            txtMatricula.setText(ss.getEntregador());
            JOptionPane.showMessageDialog(null, " Achou!.");      
            
            
        }  
    }//GEN-LAST:event_btnRastrearActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmLoginUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmLoginUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmLoginUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmLoginUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FrmLoginUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAcessar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRastrear;
    private javax.swing.JLabel lblCodigoRastreioEntrega;
    private javax.swing.JLabel lblMatricula;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JPanel pnlLoginUsuario;
    private javax.swing.JPasswordField pwdSenha;
    private javax.swing.JSeparator sepLogin;
    private javax.swing.JTextField txtCodigoRastreioEntrega;
    private javax.swing.JTextField txtMatricula;
    // End of variables declaration//GEN-END:variables
}
